﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VISS.API.Users;
using NCT.Framework;
using VISS.Web.Frontend;
using System.Collections.Generic;

namespace VISS.Web.Frontend.Modules.User.Profile
{

    public partial class ContactList : System.Web.UI.Page
    {
        protected Pager pagerrequest;
        protected Pager pagerallow;
        protected int pagenum = 1;
        protected int totalRows = 0;
        protected int TotalRecord = 0;
        protected string PagingAllowed = string.Empty;
        protected int TotalRecordAllowed = 0;

        protected string Url;
        protected bool CanDoAction;

        public static string IS_AJAX_REQUEST = "ajx";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IUserContactRequest _iUserRequest = StructureMapFactory.GetInstance<IUserContactRequest>();

                UserContext userContext = LoginFactory.GetCurrentUser();

                if (userContext == null)
                {
                    Response.Redirect("/login?url=" + AppSettings.RootURL + "profile/contactlist?");
                    return;
                }

                bool ajx = GetFormBoolean(IS_AJAX_REQUEST);

                string mod = GetFormString("mod");

                string allow = GetFormString("allow");

                if (ajx == true)
                {
                    if (mod == "paging")
                    {
                        pagenum = GetFormInt("pn");

                        if (string.Equals(allow, "request"))
                        {
                            idContactListWaiting.ContactRequestResult = _iUserRequest.GetRequestingListPaging(userContext.UserKey, pagenum, AppSettings.PageSizeOfMessage, out totalRows);

                            pagerrequest = new Pager(totalRows, AppSettings.PageSizeOfMessage, pagenum, "");

                        }
                        else if (string.Equals(allow, "allow"))
                        {
                            idContactListAllowed.ContactResult = _iUserRequest.GetAllowedListPaging(userContext.UserKey, pagenum, AppSettings.PageSizeOfMessage, out TotalRecordAllowed);

                            pagerallow = new Pager(TotalRecordAllowed, AppSettings.PageSizeOfMessage, pagenum, "");
                        }

                    }


                }

                else
                {
                    idContactListWaiting.ContactRequestResult = _iUserRequest.GetRequestingListPaging(userContext.UserKey, pagenum, AppSettings.PageSizeOfMessage, out TotalRecord);

                    pagerrequest = new Pager(TotalRecord, AppSettings.PageSizeOfMessage, pagenum, "");

                    idContactListAllowed.ContactResult = _iUserRequest.GetAllowedListPaging(userContext.UserKey, pagenum, AppSettings.PageSizeOfMessage, out TotalRecordAllowed);

                    pagerallow = new Pager(TotalRecordAllowed, AppSettings.PageSizeOfMessage, pagenum, "");

                    // SEO
                    IHeadSeo seo = this.Master as IHeadSeo;
                    seo.Title = StringResources.SEO.TitleUserMessage + " | " + userContext.FullName + " | " + StringResources.SEO.TitleDefault;
                    seo.Description = StringResources.SEO.DescriptionDefault;
                    seo.Keywords = StringResources.SEO.KeywordsDefault;
                }
            }
        }

        public static bool SafeBool(object pvObj)
        {
            if (pvObj == null || pvObj == DBNull.Value || pvObj.ToString().Trim() == String.Empty)
            {
                return false;
            }
            else
            {
                return Convert.ToBoolean(pvObj);
            }
        }
        public static int GetFormInt(string pvName)
        {
            return SafeInt(GetFormValue(pvName));
        }
        public static string GetFormString(string pvName)
        {
            return SafeString(GetFormValue(pvName)).Trim();
        }
        public static bool GetFormBoolean(string pvName)
        {
            return SafeBool(GetFormValue(pvName));
        }
        public static int SafeInt(object pvObj)
        {
            if (pvObj == null || pvObj == DBNull.Value || pvObj.ToString().Trim() == String.Empty)
            {
                return 0;
            }
            else
            {
                return (int)Convert.ToInt32(pvObj);
            }
        }
        public static string SafeString(object pvObj)
        {
            if (pvObj == null || pvObj == DBNull.Value || pvObj.ToString().Trim() == String.Empty)
            {
                return "";
            }
            else
            {
                return Convert.ToString(pvObj);
            }
        }
        public static string GetFormValue(string pvName)
        {
            return HttpContext.Current.Request.Form[pvName];
        }


        protected override void Render(HtmlTextWriter writer)
        {
            bool ajx = GetFormBoolean(IS_AJAX_REQUEST);
            string mod = GetFormString("mod");
            string allow = GetFormString("allow");
            if (ajx && mod == "paging")
            {
                if (string.Equals(allow, "request"))
                {
                    divSearchResult.RenderControl(writer);
                }
                else if (string.Equals(allow, "allow"))
                {
                    divSearchResultAllow.RenderControl(writer);
                }

            }
            else
            {
                base.Render(writer);
            }
        }


    }
}
